using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XProject.DAO;
using XProject.Entity;
using XProject.Util;
namespace XProject.GUI
{
    public partial class PhoneManager : Form
    {
        #region fields
        PhoneTypeDAO daoPhoneType = new PhoneTypeDAO();
        PhoneDAO daoPhone = new PhoneDAO();
        Contact contact = null;
        #endregion
        public PhoneManager()
        {
            InitializeComponent();
        }

        #region properties
        public Contact Contact
        {
            get { return contact; }
            set { contact = value; }
        }
        #endregion

        #region methods
        public void AddListItem(Phone phone) {
            ListViewItem item = new ListViewItem(phone.PhoneNumber);
            item.SubItems.Add(phone.PhoneType.TypeName);
            item.SubItems.Add(phone.Note);
            item.Tag = phone;
            lsvPhoneList.Items.Add(item);
        }
        public bool IsValidInput()
        {
            if( mskPhoneNumber.Text == "") return false;
            if (cmbPhoneType.SelectedIndex < 0) return false;
            return true;
            
        }
        #endregion
        private void PhoneManager_Load(object sender, EventArgs e)
        {
            IList<PhoneType> lstPhoneType = daoPhoneType.SelectAll();
            foreach(PhoneType type in lstPhoneType){
            ListItem item=new ListItem(type.TypeName,type);
            cmbPhoneType.Items.Add(item);
            }
            if(this.contact!=null)
            foreach (Phone phone in this.Contact.Phones)
            {
                AddListItem(phone);
            }
        }
        /// <summary>
        /// method for add a phone and update to database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (IsValidInput())
            {
                ListItem item = cmbPhoneType.SelectedItem as ListItem;
                PhoneType type = item.Tag as PhoneType;
                Phone phone = new Phone(-1,mskPhoneNumber.Text, type, txtNote.Text);
                if (contact.AddPhoneNumber(phone) > 0)
                {
                    MessageBox.Show("Successed");
                    AddListItem(phone);
                }
                else MessageBox.Show(daoPhone.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else MessageBox.Show("Please input required information");
        }
        /// <summary>
        /// method for update a phone
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (lsvPhoneList.SelectedItems.Count <= 0)
            {
                MessageBox.Show("Please select an item");
                return;
            }
            if (!IsValidInput())
            {
                MessageBox.Show("Please input required information");
                return;
            }
            Phone phone = lsvPhoneList.SelectedItems[0].Tag as Phone;
            phone.PhoneNumber = mskPhoneNumber.Text;
            ListItem cmiPhoneType = cmbPhoneType.SelectedItem as ListItem;
            PhoneType type = cmiPhoneType.Tag as PhoneType;
            phone.PhoneType = type;
            phone.Note = txtNote.Text;
            if (daoPhone.Update(phone) > 0)
            {
                MessageBox.Show("Updated successfully");
                lsvPhoneList.SelectedItems[0].SubItems[0].Text = phone.PhoneNumber;
                lsvPhoneList.SelectedItems[0].SubItems[1].Text = phone.PhoneType.TypeName;
                lsvPhoneList.SelectedItems[0].SubItems[2].Text = phone.Note;

            }
            else MessageBox.Show(daoPhone.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
/// <summary>
/// method to display details of selected item in list view
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        private void lsvPhoneList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lsvPhoneList.SelectedIndices.Count <= 0) return;
            Phone phone = lsvPhoneList.SelectedItems[0].Tag as Phone;
            mskPhoneNumber.Text = phone.PhoneNumber;
            cmbPhoneType.Text = phone.PhoneType.TypeName;
            txtNote.Text = phone.Note;
            
        }
        /// <summary>
        /// Method to delete a phone number and update to database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lsvPhoneList.SelectedItems.Count <= 0)
            {
                MessageBox.Show("Please select an item");
                return;
            }
            if (MessageBox.Show("Are you sure want to remove the selected phone number?","Confirm",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2) != DialogResult.Yes) return;
            Phone phone = lsvPhoneList.SelectedItems[0].Tag as Phone;
            if (contact.RemovePhoneNumber(phone) > 0)
            {
                MessageBox.Show("Deleted succesfully");
                lsvPhoneList.Items.Remove(lsvPhoneList.SelectedItems[0]);
            }
            else MessageBox.Show(daoPhone.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }        
    }
}